<?php

use yii\grid\GridView;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->registerJsFile('/js/layer.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile('/js/modal.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

$this->title = '数据还原';
$this->params['breadcrumbs'][] = ['label'=>'数据库管理'];
$this->params['breadcrumbs'][] = $this->title;
?>
    <div class="config-index">
        <div class="box box-primary">
            <div class="box-body">
                <?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'columns' => [
                        [
                            'attribute' => 'time',
                            'label' => '备份名称',
                            'value' => function ($model) {
                                return date('Ymd-His', $model['time']);
                            }
                        ],
                        'part:text:卷数',
                        'compress:text:压缩',
                        [
                            'attribute' => 'size',
                            'label' => '数据大小',
                            'value' => function ($model) {
                                return Yii::$app->formatter->asShortSize($model['size']);
                            }
                        ],
                        //            'create_time:text:备份时间',
                        [
                            'class' => 'yii\grid\ActionColumn',
                            'template' => '{a} &nbsp;&nbsp; {b}',
                            'buttons' => [
                                'a' => function ($url, $model, $key) {
                                    return Html::a('还原',
                                        ['re-init', 'time' => $model['time']],
                                        ['class' => 'db-import']
                                    );
                                },
                                'b' => function ($url, $model, $key) {
                                    return Html::a('删除',
                                        ['del'],
                                        [
                                            'data-method' => 'post',
                                            'data-ajax' => 1,
                                            'data-params' => ['time' => $model['time']],
                                            'data-confirm' => '删除后不能恢复,确定要删除吗?',
                                            'data-refresh' => '1',
                                            'data-time' => $model['time']
                                        ]
                                    );
                                }
                            ]
                        ],
                    ],
                ]); ?>
            </div>
        </div>
    </div>
<?php $this->beginBlock('js') ?>
    <script type="text/javascript">
        $(".db-import").click(function () {
            if (confirm('您确定要还原吗？')) {
                var self = this, status = ".";
                $.get(self.href, success, "json");
                window.onbeforeunload = function () {
                    return "正在还原数据库，请不要关闭！"
                }
                return false;

                function success(data) {
                    if (data.status) {
                        if (data.gz) {
                            data.info += status;
                            if (status.length === 5) {
                                status = ".";
                            } else {
                                status += ".";
                            }
                        }
                        $(self).parent().prev().text(data.info);
                        if (data.part) {
                            $.get('<?= \yii\helpers\Url::to(['re-start'])?>',
                                {"part": data.part, "start": data.start},
                                success,
                                "json"
                            );
                        } else {
                            window.onbeforeunload = function () {
                                return null;
                            }
                        }
                    } else {
                        $.modal.error(data.info);
                    }
                }
            } else {
                return false;
            }
        });
        $("[data-method=post]").click(function () {
            if (confirm('删除后不能恢复,确定要删除吗?')) {
                $.post($(this).attr('href'), {'time': $(this).attr('data-time')}, function (result) {
                    alert(result.message);
                    window.location.reload();
                });
            }
            return false;
        });
    </script>
<?php $this->endBlock('js') ?>